﻿using System.Collections.Generic;
using System.Linq;
using ZhaJinHua.Data.Entities;

namespace ZhaJinHua.Data
{
    public enum GameStatus
    {
        /// <summary>
        /// 已停止
        /// </summary>
        Stoped=0,
        /// <summary>
        /// 正在进行
        /// </summary>
        InProgress =1
    }
    public class Game
    {
        public List<UserInfo> UserList { get;private  set; }
        private double _di;
        private Dictionary<string, List<double>> _currentPoints;

        /// <summary>
        /// 开启一场游戏
        /// </summary>
        public Game(List<UserInfo> users,double di)
        {
            UserList = users ?? new List<UserInfo>();
            _di = di;

            Init();
        }


        public GameStatus Status { get;private set; }

        /// <summary>
        /// 开始游戏
        /// </summary>
        public void Start()
        {
            Init();
            Status = GameStatus.InProgress;
        }

        /// <summary>
        /// 结算
        /// </summary>
        public void Balance(string winUserName)
        {
            if (Status == GameStatus.InProgress)
            {
                if (_currentPoints.ContainsKey(winUserName))
                {
                    double totalWin = 0;
                    foreach (var currentPoint in _currentPoints)
                    {
                        if (currentPoint.Key != winUserName)
                        {
                            totalWin += currentPoint.Value.Sum();
                            var loseUser = UserList.FirstOrDefault(x => x.RealName == currentPoint.Key);
                            if (loseUser != null)
                            {
                                loseUser.Lose(currentPoint.Value.Sum());
                            }
                        }
                    }
                    var winUser = UserList.FirstOrDefault(x => x.RealName == winUserName);
                    if (winUser != null)
                    {
                        winUser.Win(totalWin);
                    }
                }
            }
            Init();
        }
        /// <summary>
        /// 流局
        /// </summary>
        public void Lose()
        {
            Init();
        }

        /// <summary>
        /// 初始化/清场
        /// </summary>
        private void Init()
        {
            _currentPoints=new Dictionary<string, List<double>>();
            foreach (var user in UserList)
            {
                _currentPoints.Add(user.RealName,new List<double>());
            }
            //设置状态为停止
            Status = GameStatus.Stoped;
        }

        public void RemoveUser(UserInfo user)
        {
            UserList.RemoveAll(x=>x.RealName == user.RealName);
        }

        public void AddUser(UserInfo user)
        {
            if (UserList.All(x => x.RealName != user.RealName))
            {
                UserList.Add(user);
            }
        }

    }

    //public enum GameUserStatus
    //{
    //    Out = 0,
    //    In = 1
    //}

    //public class GameUser
    //{
    //    public GameUserStatus Status { get; private set; }

    //    public List<double> Money { get; private set; }
    //}
}